package com.one.efaimaly.order.ui.binder;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.one.common.config.MyContact;
import com.one.common.config.RouterPath;
import com.one.common.manager.RouterManager;
import com.one.common.model.http.NetConstant;
import com.one.common.utils.StringUtils;
import com.one.common.utils.SystemUtils;
import com.one.common.utils.list.ListUtils;
import com.one.common.view.multitytype.adapter.BaseItemBinder;
import com.one.common.view.multitytype.adapter.BaseViewHolderMulti;
import com.one.common.view.photolayout.SortableNinePhotoLayout;
import com.one.common.view.preview.PreviewPicActivity;
import com.one.efaimaly.R;
import com.one.efaimaly.order.model.extra.AddRemarkExtra;
import com.one.efaimaly.order.model.extra.UploadImgExtra;
import com.one.efaimaly.order.model.item.OrderDetailItem;
import com.one.efaimaly.order.presenter.OrderStateHandle;
import com.one.efaimaly.order.ui.AddRemarkActivity;
import com.one.efaimaly.order.ui.OrderDetailActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * creator: zhulunjun
 * time:    2018/8/9
 * describe:
 */
public class OrderDetailBinder extends BaseItemBinder<OrderDetailItem> {
    public OrderDetailBinder() {
        super(R.layout.item_order_detail);
    }

    @Override
    protected void bindView(@NonNull BaseViewHolderMulti holder, @NonNull OrderDetailItem item) {
        holder.setText(R.id.tv_state, OrderStateHandle.getStateStr(item.getOrder_state()));
        holder.setText(R.id.tv_order_no, "订单编号："+item.getOrder_no());
        if(StringUtils.isNotBlank(item.getCreate_time())) {
            holder.setText(R.id.tv_time, "创建时间："+item.getCreate_time().substring(0, item.getCreate_time().length()-3));
        } else {
            holder.getView(R.id.tv_time).setVisibility(View.GONE);
        }
        // 开始时间和结束时间
        if(StringUtils.isNotBlank(item.getService_start_time()) && StringUtils.isNotBlank(item.getService_end_time())){
//            String time = TimeUtils.getServiceStarEndTime(item.getService_start_time(), item.getService_end_time());
            String time = item.getService_start_time().substring(0, 10);
            String timeArea = item.getService_start_time().substring(11, 16) + "-" + item.getService_end_time().substring(11, 16);
            holder.setText(R.id.tv_service_time,  "服务时间 "+time + "  " + timeArea);
//            holder.setText(R.id.tv_service_time, "服务时间 "+time);
        }
        if(item.getMaintailOrderAddressBean() != null) {
            holder.setText(R.id.tv_contact, "联系人：" + item.getMaintailOrderAddressBean().getAddress_name());
            holder.setText(R.id.tv_contact_phone, item.getMaintailOrderAddressBean().getAddress_mobile());
            holder.setText(R.id.tv_address, "服务地址：" + item.getMaintailOrderAddressBean().getAddress());
            holder.setText(R.id.tv_xiaoqu, "小区：" + item.getMaintailOrderAddressBean().getAddress_detailed());
        }
        if(StringUtils.isNotBlank(item.getRemark()))
            holder.setText(R.id.tv_service_remark,item.getRemark());
        else
            holder.setText(R.id.tv_service_remark,"暂无备注");
        //添加备注页面跳转
        LinearLayout llRemark = holder.getView(R.id.ly_service_remark);
        llRemark.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RouterManager.getInstance().go(RouterPath.ADD_REMARK,
                        new AddRemarkExtra(item.getOrder_id(),item.getRemark()));
            }
        });

        //拨打电话
        TextView tvPhone = holder.getView(R.id.tv_contact_phone);
        tvPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SystemUtils.call(mContext, item.getMaintailOrderAddressBean().getAddress_mobile());
            }
        });

        // 维修项目
        TextView tvArea1,tvArea2,tvArea3;
        tvArea1 = holder.getView(R.id.tv_area_1);
        tvArea2 = holder.getView(R.id.tv_area_2);
        tvArea3 = holder.getView(R.id.tv_area_3);
        LinearLayout llArea = holder.getView(R.id.ll_area);
        tvArea1.setVisibility(View.GONE);
        llArea.setVisibility(View.GONE);
        tvArea3.setVisibility(View.GONE);
        if (StringUtils.isNotBlank(item.getParts_names())) {
            List<String> list = ListUtils.arrayToList(item.getParts_names().split(","));
            if(ListUtils.isNotEmpty(list)) {
                tvArea1.setVisibility(View.VISIBLE);
                tvArea1.setText(list.get(0));
                if (list.size() > 1) {
                    llArea.setVisibility(View.VISIBLE);
                    tvArea2.setText(list.get(1));
                    if(list.size()>2){
                        tvArea3.setVisibility(View.VISIBLE);
                        tvArea3.setText(list.get(2));
                    }
                }
            }
        }

        //图片
        SortableNinePhotoLayout photoLayout = holder.getView(R.id.snp_photo);
        photoLayout.setVisibility(View.GONE);
        if(StringUtils.isNotBlank(item.getMaintail_imgs())){
            ArrayList<String> list = ListUtils.arrayToAreList(item.getMaintail_imgs().split(","));
            ArrayList<String> data = new ArrayList<>();
            assert list != null;
            for (String path : list){
                data.add(NetConstant.IMG_HOST+path);
            }
            if(ListUtils.isNotEmpty(list)) {
                photoLayout.setVisibility(View.VISIBLE);
                photoLayout.setData(data);
            }
        }
        photoLayout.setDelegate(delegate);

        holder.setText(R.id.tv_service_des, item.getMaintail_remark());
        if(item.getIs_guarantee()){
            holder.getView(R.id.iv_bao).setVisibility(View.VISIBLE);
        } else {
            holder.getView(R.id.iv_bao).setVisibility(View.GONE);
        }

    }

    SortableNinePhotoLayout.Delegate delegate = new SortableNinePhotoLayout.Delegate() {
        @Override
        public void onClickAddNinePhotoItem(SortableNinePhotoLayout sortableNinePhotoLayout, View view, int position, ArrayList<String> models) {

        }

        @Override
        public void onClickDeleteNinePhotoItem(SortableNinePhotoLayout sortableNinePhotoLayout, View view, int position, String model, ArrayList<String> models) {

        }

        @Override
        public void onClickNinePhotoItem(SortableNinePhotoLayout sortableNinePhotoLayout, View view, int position, String model, ArrayList<String> models) {
            // 点击图片
            PreviewPicActivity.starPreview(mContext, models, position);
        }

        @Override
        public void onNinePhotoItemExchanged(SortableNinePhotoLayout sortableNinePhotoLayout, int fromPosition, int toPosition, ArrayList<String> models) {

        }
    };
}
